package com.liyuhan.onlinetaxibackground.Service.impl;

import com.google.gson.Gson;
import com.liyuhan.onlinetaxibackground.POJO.CompletedOrder;
import com.liyuhan.onlinetaxibackground.POJO.ReceivedOrder;
import com.liyuhan.onlinetaxibackground.Repository.CompletedOrderRepository;
import com.liyuhan.onlinetaxibackground.Repository.ReceivedOrderRepository;
import com.liyuhan.onlinetaxibackground.Service.CompletedOrderService;
import com.liyuhan.onlinetaxibackground.Util.GetRequestBody;
import com.liyuhan.onlinetaxibackground.dto.DataRequest;
import com.liyuhan.onlinetaxibackground.dto.DataResponse;
import com.liyuhan.onlinetaxibackground.dto.orderDto.CompletedOrderDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class CompletedOrderImpl implements CompletedOrderService {

    @Autowired
    private ReceivedOrderRepository receivedOrderRepository;

    @Autowired
    private CompletedOrderRepository completedOrderRepository;

    // 响应体参数
    private Map<String, String> map = new HashMap<>();

    @Override
    public DataResponse getCompletedOrder(String request) {
        DataRequest requestBody = GetRequestBody.getRequestBody(request);
        map.clear();

        String orderId = requestBody.getData().get("orderId").toString();

        // 获取完成的订单
        CompletedOrder completedOrder = completedOrderRepository.findCompletedOrderByReceivedOrder(receivedOrderRepository.findById(Integer.valueOf(orderId)).get()).get();

        // 构建数据交换体
        CompletedOrderDto completedOrderDto = new CompletedOrderDto(
                completedOrder.getId(),
                completedOrder.getReceivedOrder(),
                completedOrder.getState(),
                completedOrder.getArriveTime()
        );

        Gson gson = new Gson();
        String orderStr = gson.toJson(completedOrderDto);

        map.put("order", orderStr);

        return new DataResponse("200", map);
    }

    @Override
    public void payOrder(String request) {
        DataRequest requestBody = GetRequestBody.getRequestBody(request);

        String orderId = requestBody.getData().get("orderId").toString();
        String pay = requestBody.getData().get("pay").toString();

        CompletedOrder completedOrder = completedOrderRepository.findById(Integer.valueOf(orderId)).get();

        completedOrder.setState("8");
        completedOrder.setPay(Double.valueOf(pay));

        // 保存订单
        completedOrderRepository.save(completedOrder);
    }

    @Override
    public void deleteOrder(String request) {
        DataRequest requestBody = GetRequestBody.getRequestBody(request);

        String orderId = requestBody.getData().get("orderId").toString();

        CompletedOrder completedOrder = completedOrderRepository.findById(Integer.valueOf(orderId)).get();

        completedOrder.setState("9");

        // 保存订单
        completedOrderRepository.save(completedOrder);
    }

    @Override
    public DataResponse getOrderState(String request) {
        DataRequest requestBody = GetRequestBody.getRequestBody(request);

        Integer orderId = Integer.valueOf(requestBody.getData().get("orderId").toString());

        // 根据id获取订单
        ReceivedOrder order = receivedOrderRepository.findById(orderId).get();

        if (completedOrderRepository.findCompletedOrderByReceivedOrder(order).isPresent()) {
            map = new HashMap<>();
            map.put("state", "7");
            // 将订单状态回传
            return new DataResponse("200", map);
        }

        return new DataResponse("400", null);

    }
}
